#define PI 3.141592653589793
#define TAU 6.283185307179586

float polygon(vec2 p, int vertices, float size) {
    float a = atan(p.x, p.y) + 0.2;
    float b = 6.28319 / float(vertices);
    return cos(floor(0.5 + a / b) * b - a) * length(p) - size;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    float time = iTime * 0.5;
    
    //////////////////////////////////////////////////////
    // Create tunnel coordinates (p) and remap to normal coordinates (uv)
    // Technique from @iq: https://www.shadertoy.com/view/Ms2SWW
	// and a derivative:   https://www.shadertoy.com/view/Xd2SWD
    vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;		// normalized coordinates (-1 to 1 vertically)
    vec2 uvOrig = p;
    // added twist by me ------------
    float rotZ = 1. - 0.07 * sin(1. * sin(length(p * 1.5)));
    p *= mat2(cos(rotZ), sin(rotZ), -sin(rotZ), cos(rotZ));
	//-------------------------------
    float a = atan(p.y,p.x);												// angle of each pixel to the center of the screen
    float rSquare = pow( pow(p.x*p.x,4.0) + pow(p.y*p.y,4.0), 1.0/8.0 );	// modified distance metric (http://en.wikipedia.org/wiki/Minkowski_distance)
    float rRound = length(p);
    float r = mix(rSquare, rRound, 0.5 + 0.5 * sin(time * 2.)); 			// interp between round & rect tunnels
    vec2 uv = vec2( 0.3/r + time, a/3.1415927 );							// index texture by (animated inverse) radious and angle
    //////////////////////////////////////////////////////

    // subdivide to grid
    uv += vec2(0., 0.25 * sin(time + uv.x * 2.));			// pre-warp
    vec2 uvDraw = fract(uv * 7. + 5. * sin(time)) - 0.5;	// create grid

    // alter polygon direction by pre-rotating coords
    float rot = PI/2.;
    uvDraw *= mat2(cos(rot), sin(rot), -sin(rot), cos(rot));

    // draw arrow
    float antialias = 8./iResolution.y;
    vec2 rectPos = uvDraw + vec2(0., -0.08);
    float col = smoothstep(antialias, 0., polygon(rectPos, 4, 0.12)); 	 // antialiased rect
    vec2 triPos = uvDraw + vec2(-0.047, 0.15);
    col = max(col, smoothstep(antialias, 0., polygon(triPos, 3, 0.12))); // antialiased triangle

    // darker towards center, light towards outer
    col = col * r * 1.5;
    col += 0.15 * length(uvOrig);
    fragColor = vec4(vec3(col), 0.);
}